<?php
	
    class UserInfoEngine
    {
		private $template;
		private $dbProvider;
		
		public function __construct()
        {
            $this->template = new TemplateEngine();
			$this->dbProvider = new UserDataProvider();
        }
		
		public function checkSetId()
		{
			$isSet = isset( $_GET['id'] );
			return $isSet;
		}
		
		public function checkSetEmail()
		{
			$isSet = isset( $_POST['email_find'] );
			return $isSet;
		}
		
		public function getEmailFromId( $userId )
        {
			$email = $this->dbProvider->getEmailById( $userId );
			if( !$email )
			{
				throw new Exception( $this->template->convertToHtml( "This id is not exist!" ) );
				return NULL;
			}
			else
			{
				return $email;
			}
        }

        public function isDataTaken( $email, &$result )
        {
            $isValid = ( !empty( $email ) );
			if ( $isValid )
			{
				if ( $this->findInBase( $firstName, $lastName, $email, $password, $sex, $month, $day, $year ) )
				{
					$result .= $this->printFormData( $firstName, $lastName, $email, $password, $sex, $month, $day, $year );
					return true;
				}
				return false;
			}
			else if ( !$this->checkSetId() )
			{
				throw new Exception( $this->template->convertToHtml( "Not all data is entered!" ) );
				return false;
			}
        }

        private function readFromFile( &$firstName, &$lastName, &$email, &$password, &$sex, &$month, &$day, &$year )
        {
            $filename = strtolower( $email ).".txt";
            if ( is_file( $filename ) )
            {   
                $file = fopen( $filename, "r" );
                        
                $firstName = fgets( $file, 255 );
                $lastName = fgets( $file, 255 );
                $email = fgets( $file, 255 );
                $password = fgets( $file, 255 );
                $sex = fgets( $file, 255 );
                $month = fgets( $file, 255 );
                $day = fgets( $file, 255 );
                $year = fgets( $file, 255 );
                
                fclose( $file );
				return true;
            }
            else
            {
                throw new Exception( $this->template->convertToHtml( "This email is not exist!" ) );
				return false;
            }
        }
        
        private function printFormData( $firstName, $lastName, $email, $password, $sex, $month, $day, $year )
        {
            $vars = array();
	
			$vars['firstName'] = $firstName;
			$vars['lastName'] = $lastName;
			$vars['email'] = $email;
			$vars['password'] = $password;
			$vars['sex'] = $sex;
			$vars['month'] = $month;
			$vars['day'] = $day;
			$vars['year'] = $year;
            
            return getView('templates/user_data.tpl', $vars);;
        }
        
        private function findInBase( &$firstName, &$lastName, &$email, &$password, &$sex, &$month, &$day, &$year )
        {
            $email = strtolower( $email );
            $resultArr = $this->dbProvider->getUserDataFromDb( $email );
           
            if ( $resultArr )
            {
                $firstName = $resultArr[0]['first_name'];
                $lastName = $resultArr[0]['last_name'];
                $email = $resultArr[0]['email'];
                $password = $resultArr[0]['password'];
                $sex = $resultArr[0]['sex'];
                $month = $resultArr[0]['birth_month'];
                $day = $resultArr[0]['birth_day'];
                $year = $resultArr[0]['birth_year'];
				return true;
            }
            else
            {
				throw new Exception( $this->template->convertToHtml( "This email is not exist!" ) );
				return false;
            }
        }
		
	}
?>